-- Server Side --


-- This will manually change the team you are on.
function TDMConCom(ply, cmds, args)
if !ply:IsSuperAdmin() || args[1] == nil then return end
RunConsoleCommand(args[1], args[2], args[3], args[4], args[5], args[6], args[7])
end
concommand.Add( "TDMConCom", TDMConCom )


-- Player can not use kill in console to quick team change.
function BlockSuicideTDM(ply)
umsg.Start( "TDM_Can_Kill_Self", ply)
umsg.End()
	return false
end
hook.Add( "CanPlayerSuicide", "BlockSuicideTDM", BlockSuicideTDM )


-- This will manually change the team you are on.
function TDMChangeTeam(ply, cmds, args)
if args[1] == nil then return end -- If arguments are empty, just end there
	for k,v in pairs(AddTeams) do -- Checks all the teams
		if v.tid == tonumber(args[1]) then -- If the team ID is the same as selected
			ply:SetTeam(tonumber(args[1])) -- Sets the team
			
			ply:StripWeapons()
			ply:StripAmmo()
			
			ply:SetPData("ReturnLastUsedWeapon", "nil")
			
			local GetAllWeapons2 = sql.QueryValue( "SELECT Weapons FROM apple_tdm_teamcreation22 WHERE ID = "..tonumber(args[1]).."" ) -- Get all models from selected team 
			local GetAllSepWeapons2 = string.Explode(",",GetAllWeapons2) -- Separates all the models
			for k, v in pairs( GetAllSepWeapons2 ) do
			ply:Give(v) -- Give all the weapons of the team.
			end
			
			for g, h in pairs(TeamModels2) do -- This will check to see if the model is a team model
				if h.id == ply:Team() then -- Checks Team and Compares with Models of That Team	
					local GetAllSepModels = string.Explode(",",h.tmodels) -- Separates all the models
					local TeamModel = table.Random(GetAllSepModels) -- Chooses a random model from the selected team
					ply:SetModel(TeamModel) -- Sets the model of the team
					timer.Simple(0.35, function() -- Sets delay so it doesn't go too fast when refreshing list
					if !ply:IsValid() then return end
					umsg.Start("apple_tdm_changeteam", ply) -- Update the listing
						umsg.Short(ply:Team())
					umsg.End()
						for k,v in pairs(player.GetAll()) do -- Because the team has just changed, and a player may have menu open, we need to get all
							umsg.Start("apple_tdm_updatelistp", v) -- Update the listing
							umsg.End()
						end
					end)
				end
			end
		end
	end
end
concommand.Add( "TDMChangeTeam", TDMChangeTeam )


-- This will manually change the model you are using.
function TDMChangeModel(ply, cmds, args)
if args[1] == nil then return end -- If arguments are empty, just end there
	if util.IsValidModel(tostring(args[1])) == false then -- Checks to see if model is valid
			umsg.Start("apple_tdm_attemptmodelfail", ply) -- If Model is Invalid, Will give Kick Reason
			umsg.End()
			timer.Simple(5, function() -- Just to make sure they can read it, I put a delay
			if !ply:IsValid() then return end
				ply:Kick("[TDM] Attempted to Hack Models") -- Was considering making this a ban if tried more than ten times.
			end)
	return end
	
	for k, v in pairs(TeamModels2) do -- This will check to see if the model is a team model
		if v.id == ply:Team() then -- Checks Team and Compares with Models of That Team
			local exp = string.Explode(",",v.tmodels) -- Because the models are in a table, I need list all as strings
			for r, b in pairs(exp) do -- Umm... Separates all of the models
				if b == tostring(args[1]) then -- If the model is the same as the selected model it shall continue
					ply:SetModel(tostring(args[1])) -- Sets the Model is everything is good :)
				end
			end
		end
	end
end
concommand.Add( "TDMChangeModel", TDMChangeModel )